<?php

class MStorageEditHistory extends MY_Model
{
    protected $table = 'storage_edit_history';
    private $filter = array();

    function __construct()
    {
        parent::__construct();
    }

    public static function getOrderType($orderType = NULL)
    {
        $define = array(
            self::ORDER_ID_DESC => 'ID倒序',
            self::ORDER_ID_ASC => 'ID升序'
        );
        if ($orderType === NULL) {
            return $define;
        } else {
            return key_exists($orderType, $define) ? $define[$orderType] : '';
        }
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATUS_DISABLED => '作废', self::STATUS_SUCCESS => '成功');
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public function getList($limit, $sort = false)
    {
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($this->table);

        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();
        if ($sort) {
            switch ($sort) {
                case self::ORDER_ID_DESC:
                    $this->db->order_by('id', 'desc');
                    break;
                case self::ORDER_ID_ASC:
                    $this->db->order_by('id', 'asc');
                    break;
            }
        } else {
            $this->db->order_by('id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }

    public function getCount()
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table );
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getAllHis($route,$effect_id){
        $this->db->select('l.*,a.name');
        $this->db->from($this->table.' l');
        $this->db->join('admins a','l.user_id = a.id');
        $this->db->where('l.route = '.$route);
        $this->db->where('l.effect_id = '.$effect_id);
        $query = $this->db->get();
        return $query->result();
    }
}
